package utils

import "fmt"

/*
*

	key, val 不能包含
*/
func DataPacakageDecodeEncode(body []byte, initBuf []byte, headkvPairs ...string) (newBuf []byte) {
	newBuf = initBuf
	l := len(headkvPairs)
	if l%2 != 0 { // 长度必须是双数
		panic("headkvPairs 长度必须是双数")
	}

	i := 0
	for i < l {
		key := headkvPairs[i]
		val := headkvPairs[i+1]
		newBuf = append(newBuf, []byte(fmt.Sprintf("%s:%s\n", key, val))...)
		i += 2
	}
	bodyl := len(body)
	if bodyl > 0 {
		newBuf = append(newBuf, []byte(fmt.Sprintf("len:%d\n\n", bodyl))...)
		newBuf = append(newBuf, body...)
	} else {
		newBuf = append(newBuf, '\n')
	}
	return
}
